import { SAVE_BUY_CONFIRM, CLEAR_BUY_LIST } from './../types'
import { getStore, setStore, removeStore } from '@/utils/mUtils'

const MY_BUY_LIST = 'MY_BUY_LIST'

const state = {
	buyList: getStore(MY_BUY_LIST) ? JSON.parse(getStore(MY_BUY_LIST)) : []
}

const getters = {
	buyList: state => {
		return state.buyList
	}
}

const actions = {
	saveBuyConfirm ({commit}, data) {
		commit(SAVE_BUY_CONFIRM, data)
	},
	clearBuyList ({commit}) {
		commit(CLEAR_BUY_LIST)
	}
}

const mutations = {
	[SAVE_BUY_CONFIRM] (state, data) {
		state.buyList = state.buyList.concat(data)
		setStore(MY_BUY_LIST, state.buyList)
	},
	[CLEAR_BUY_LIST] (state) {
		state.buyList = []
		removeStore(MY_BUY_LIST)
	}
}

export default {
	state,
	getters,
	mutations,
	actions
}